
<!-- 销售统计 -->
<template>
  <div>
    <div class="box">
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="9">
          <div class="left">
            <span class="time">时间:</span>
            <el-date-picker
              v-model="value1"
              type="daterange"
              range-separator="~"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="editDate"
            />
          </div>
        </el-col>
        <el-col :span="4.5">
          <div class="right">
            <el-tabs v-model="activeName" class="custom-tabs" type="card" @tab-click="handleClick">
              <el-tab-pane label="销售金额" name="first" />
              <el-tab-pane label="合同数量" name="second" />
            </el-tabs>
          </div>
        </el-col>
      </el-row>

    </div>
    <!-- 图例 -->
    <div v-show="isShow" class="legend">
      <div id="charts" style="width: 1350px; height: 300px;" />
    </div>
    <div v-show="onShow" class="legend-two">
      <div id="twoCharts" style="width: 1350px; height: 300px;" />
    </div>
    <div>
      <div class="tap">
        <span class="text">销售统计报表</span>

        <el-row>
          <el-button type="primary" class="export">导出</el-button>
        </el-row>
      </div>
      <div class="floor">
        <span>高级搜索</span>
      </div>
      <!-- 搜索区域 -->
      <div class="search">
        <el-form :model="params">
          <el-row class="search_row">
            <el-col :span="6">
              <el-form-item label="排行：">
                <el-input v-model="params.sort" placeholder="请输入所属渠道" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="所属渠道：">
                <el-input v-model="params.deptName" placeholder="请输入所属渠道" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="归属人：">
                <el-select v-model="value" placeholder="请选择活动区域" style="width: 200px;">
                  <el-option label="区域一" value="shanghai" />
                  <el-option label="区域二" value="beijing" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="search_btn">
                <el-button class="cus-reset-btn">重置</el-button>
                <el-button class="cus-search-btn" type="primary" @click="search">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 列表区域 -->
      <div>
        <el-table class="cus-table-box" stripe :data="list">
          <el-table-column type="selection" width="62" align="center" />
          <el-table-column label="排行" align="center" prop="sort" />
          <el-table-column label="归属渠道" align="center" prop="deptName" />
          <el-table-column label="合同数量" align="center" prop="contractNum" />
          <el-table-column label="成交金额" align="center" prop="contractOrder" />
        </el-table>
        <div class="block">
          <el-pagination
            :current-page="params.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getSaleTimeApi, getsaleListApi } from '@/api/sale'
// const echarts = require('echarts')
export default {
  data() {
    return {
      value: '',
      value1: ['2023-07-05', '2023-07-06'],
      activeName: 'first',
      isShow: true,
      onShow: false,
      newXAxis: ['2023-07-05', '2023-07-06'],
      params: {
        page: 1,
        pageSize: 10,
        deptName: '',
        sort: null
      },
      list: [],
      total: 0

    }
  },
  mounted() {
    this.initCharts()
    this.twoCharts()
  },
  created() {
    this.getsaleList()
  },
  methods: {
    /**
     * 搜索
     */
    search() {
      this.params.page = 1
      this.getsaleList(this.params)
    },
    /**
     * 分页
     * @param {*} page
     */
    handleCurrentChange(page) {
      this.params.page = page
      this.getsaleList()
    },
    /**
     * 渲染列表
     */
    async getsaleList() {
      const { data } = await getsaleListApi(this.params)
      console.log(data)
      this.list = data.records
      this.total = data.total
    },
    /**
     * 第一个图例
     */
    initCharts() {
      const myChart = echarts.init(document.getElementById('charts'))
      // 绘制图表
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },

        // 横向行
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: this.newXAxis
          }
        ],

        yAxis: [
          {
            type: 'value',
            name: '单位(元)'
          }
        ],

        series: [
          // 行
          {
            name: '新增客户',
            type: 'line',
            stack: 'Total',
            areaStyle: {},
            emphasis: {
              focus: 'series'
            },
            data: [200000, 250000, 300000, 350000, 400000, 45000, 500000] // 总量的数据
          }
          // 列

        ]
      }
      myChart.setOption(option)
      window.addEventListener('resize', function() {
        myChart.resize()
      })
    },
    /**
     * 第二个图例
     */
    twoCharts() {
      const myChart = echarts.init(document.getElementById('twoCharts'))
      // 绘制图表
      const option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'line'
          }
        ]
      }
      myChart.setOption(option)
      window.addEventListener('resize', function() {
        myChart.resize()
      })
    },

    /**
     * 时间改变
     */
    editDate(val) {
      this.getSaleTime(val[0], val[1])
    },
    async getSaleTime(data1, data2) {
      const { data } = await getSaleTimeApi(data1, data2)
      console.log(data)
      localStorage.setItem('newXAxis', JSON.stringify(data.xAxis))
      // this.newXAxis = data.xAxis
      const res = localStorage.getItem('newXAxis')
      //
      this.newXAxis = JSON.parse(res)
      this.initCharts()
      // console.log(this.newXAxis)
    },
    /**
     * 点击切换图例
     */
    handleClick() {
      if (this.activeName === 'first') {
        this.isShow = true
        this.onShow = false
      }
      if (this.activeName === 'second') {
        this.isShow = false
        this.onShow = true
      }
    }
  }
}
</script>

<style scoped lang="scss">
.box{
    margin-top: 20px;
    margin-bottom: 100px;
    .left{
      display: inline;
    }
    .right{
      display: inline;
    }
}
 .legend{
    height: 300px;
    background-color: transparent;
    margin-bottom: 70px;
 }
 .legend-two{
  height: 300px;
  background-color: transparent;
    margin-bottom: 70px;
 }
.time{
    margin-right: 10px;
}

.tap{
    display: flex;
    height: 40px;
    margin-bottom: 10px;
    justify-content: space-between;

}
.text{
    line-height: 40px;
    margin-left: 10px;
}
.export{
  // margin-top: -50px;
  height: 40px;
  text-align: center;
  // line-height: 40px;
}
.floor{
    height: 40px;
    background-color: #f2f2f2;
    span{
        line-height: 40px;
        margin-left: 10px;
    }
}
.search{
    height: 150px;
}
.search_row{
  padding-top: 25px;
  padding-left: 50px;
}
.search_btn{
  margin-left: 100px;
}
.block{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
}
</style>
